package edu.pku.sei.mte.mtemodel.model;

import java.util.List;

import edu.pku.sei.mte.constants.COMPOSITEMODE;
import edu.pku.sei.mte.mtemodel.graph.Graph;
import edu.pku.sei.mte.mtemodel.metamodel.RTypeGraph;
import edu.pku.sei.mte.mtemodel.metamodel.RTypeReference;
import edu.pku.sei.mte.utilities.Visitor;

public class MModelGraph extends Graph<MClass,MReference> {
	private RTypeGraph typeGraph;

	public void setTypeGraph(RTypeGraph typeGraph) {
		this.typeGraph = typeGraph;
	}

	public RTypeGraph getTypeGraph() {
		return typeGraph;
	}

	@Override
	public boolean removeNode(MClass node) {
		List<MClass> cls = node.getCompositeClass();
		if(super.removeNode(node)==false) return false;
		for(MClass c : cls){
			removeNode(c);
		}
		return true;
	}

	@Override
	public boolean removeEdge(MReference edge) {
		MClass src = edge.getSource();
		MClass tar = edge.getTarget();
		RTypeReference type = edge.getType();
		
		if(super.removeEdge(edge)==false)
			return false;
		
		if(tar!=null&&type.getEndComposite()==COMPOSITEMODE.SOURCE_COMPOSITE){
			super.removeNode(tar);
		}
		if(src!=null&&type.getEndComposite()==COMPOSITEMODE.TARGET_COMPOSITE){
			super.removeNode(src);
		}
		
		return true;
	}
	
	@Override
	public Object accept(Visitor visitor) {
		return visitor.visitMModelGraph(this);
	}
}
